<?php
/* SVN FILE: $Id: tag.php 33 2007-09-18 23:08:04Z joe.vasquez $ */
/**
 * tag model
 * @filesource
 * @author			Joe Vasquez <joe.vasquez@gmail.com>
 * @copyright		Copyright (c) 2007, Joe Vasquez
 * @package			redlettr
 * @subpackage		tags
 * @since			0.1A
 * @version			0.1A  $Rev: 33 $
 * @lastmodified	$Date: 2007-09-18 23:08:04 +0000 (Tue, 18 Sep 2007) $
 * @license			http://www.gnu.org/licenses/gpl.html GNU General Public License
 */
 
class Tag extends AppModel
{
	var $name = 'Tag';
	
	var $hasAndBelongsToMany = array(
		'Scripture'	=> array(
			'className'				=> 'Scripture',
			'join_table'			=> 'scriptures_tags',
			'foreignKey'			=> 'tag_id',
			'associationForeignKey'	=> 'scripture_id',
			'unique'				=> true
		)	
	);
		
	function getTagIds($tagNames,$createNewTags=false,$generated='search')
	{
		array_walk($tagNames,'remove_slashes');
		$tagIds = $this->generateList(
			array(
				'Tag.name'	=> $tagNames
			),
			null,
			null,
			'{n}.Tag.id',
			'{n}.Tag.name'
		);
		if($createNewTags)
		{
			if(is_array($tagIds))
			{
				$newTags = array_diff($tagNames,$tagIds);
			} else {
				$newTags = $tagNames;
			}
			if(count($newTags) > 0)
			{
				foreach($newTags as $newTag)
				{
					$this->create(
						array(
							'name'		=> $newTag,
							'generated'	=> $generated
						)
					);
					$this->save();
				}
				$newTagIds = $this->generateList(
					array(
						'Tag.name'	=> $newTags
					),
					null,
					null,
					'{n}.Tag.id',
					'{n}.Tag.name'
				);
				if(is_array($tagIds))
				{
					$tagIds = array_merge($tagIds,$newTagIds);
				} else {
					$tagIds = $newTagIds;
				}
			}
		}
		return $tagIds;
	}
}?>